﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HBAClient
{
    public partial class Report : Form
    {
        public Report()
        {
            InitializeComponent();
        }

		private void Report_Load(object sender, EventArgs e)
		{
			HBADBDataContext db = new HBADBDataContext();

			var query = from balance in db.Balances
						join bank in db.Banks on balance.BankID equals bank.BankID
						where balance.UserID == Program.GetUserID()
						select new { BankName = bank.BankName, 
									 BankID   = bank.BankID };

			//dgvReport.DataSource = query.Distinct();
			cmbBank.DisplayMember = "BankName";
			cmbBank.ValueMember = "BankID";
			cmbBank.DataSource = query.Distinct();

			var query2 = from bill in db.Bills
						 where bill.UserID == Program.GetUserID()
						 select new
						 {
							 Source = bill.Source
						 };

			cmbSource.DisplayMember = "Source";
			cmbSource.ValueMember = "Source";
			cmbSource.DataSource = query2.Distinct();
            //this.reportViewer1.RefreshReport();
		}

		private void CheckDateValidation()
		{
			if (dtEndDatePick.Value.Date < dtStartDatePick.Value.Date)
			{
				MessageBox.Show("End Date cannot be earlier than Start Date");
				dtStartDatePick.Value = DateTime.Now.Date;
				dtEndDatePick.Value = DateTime.Now.Date;
			}
		}

		private void btnRun_Click(object sender, EventArgs e)
		{

			HBADBDataContext db = new HBADBDataContext();

            var query = from bill in db.Bills
                        join bank in db.Banks on bill.BankID equals bank.BankID
                        where bill.UserID == Program.GetUserID() &&
                        bill.Date >= dtStartDatePick.Value.Date &&
                        bill.Date <= dtEndDatePick.Value.Date.AddDays(1) &&
                        bill.Source.Contains(cmbSource.Text) &&
                        bank.BankID.ToString() == Convert.ToString(cmbBank.SelectedValue)
                        select new
                        {
                            //BillID = bill.BillID,
                            //BankName = bank.BankName,
                            Date = bill.Date,
                            Source = bill.Source,
                            InSum = bill.InSum,
                            OutSum = bill.OutSum,
                            Balance = bill.Balance
                        };

            // Check if return all the sources
            if (chbxAllSources.Checked)
            {
                query = from bill in db.Bills
                        join bank in db.Banks on bill.BankID equals bank.BankID
                        where bill.UserID == Program.GetUserID() &&
                        bill.Date >= dtStartDatePick.Value.Date &&
                        bill.Date <= dtEndDatePick.Value.Date.AddDays(1) &&
                        bank.BankID.ToString() == Convert.ToString(cmbBank.SelectedValue)
                        select new
                        {
                            //BillID = bill.BillID,
                            //BankName = bank.BankName,
                            Date = bill.Date,
                            Source = bill.Source,
                            InSum = bill.InSum,
                            OutSum = bill.OutSum,
                            Balance = bill.Balance
                        };
            }
                           
			try
			{
				dgvReport.DataSource = query;
			}
			catch (Exception ex)
			{
				dgvReport.DataSource = null;
			}
			
		}


		private void dtStartDatePick_CloseUp(object sender, EventArgs e)
		{
			CheckDateValidation();
		}

		private void dtEndDatePick_ValueChanged(object sender, EventArgs e)
		{
			CheckDateValidation();
		}

		private void dtEndDatePick_CloseUp(object sender, EventArgs e)
		{
			CheckDateValidation();
		}

        private void brnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void chbxAllSources_CheckedChanged(object sender, EventArgs e)
        {
            if (chbxAllSources.Checked)
            {
                cmbSource.Enabled = false;
            }
            else
            {
                cmbSource.Enabled = true;
            }
        }
        private void ChartClick()
        {
            // Build values for chart
            Dictionary<float, string> dcValues = new Dictionary<float, string>();
            string strChartName = cmbSource.Text;

            // Check if report have rows
            if (dgvReport.RowCount > 0)
            {
                //string strBillID_ToDelete = dgvReport.SelectedCells[0].Value.ToString();
                float nCurrValue = 0;
                for (int i = 0; i < dgvReport.RowCount; i++)
                {
                    nCurrValue = float.Parse(dgvReport.Rows[i].Cells[3].Value.ToString()) +
                        float.Parse(dgvReport.Rows[i].Cells[4].Value.ToString());
                    nCurrValue = Math.Abs(nCurrValue);

                    dcValues[nCurrValue] = strChartName;

                    nCurrValue = 0;
                }

                Graph formGraph = new Graph(ref dcValues);
                formGraph.GoWeb();
                formGraph.Text = "Charts";
                formGraph.Show();
            }
            else // No rows
            {
            }

        }
        private void btnChart_Click(object sender, EventArgs e)
        {
            if (!chbxAllSources.Checked)
            {
                ChartClick();
            }
        }
    }
}
